//
//  DispatchViewController.swift
//  test-ios
//
//  Created by wyz5204 on 2023/3/11.
//

import UIKit

class DispatchViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        let queue = DispatchQueue.init(label: "blog 1 ")
        
        let queue1 = DispatchQueue.init(label: "blog 2 ")
        let group =  DispatchGroup.init()
        queue.async(group: group, execute: DispatchWorkItem.init(block: {
            print("1 asdasdad")
        }))
        queue1.async(group: group, execute: DispatchWorkItem.init(block: {
            print("2 asdasdad")
        }))
        testGCD()
    }
    
    private var button : UIButton = {
        return UIButton().then { view in
            view.setTitleColor(UIColor.red, for: UIControl.State.normal)
            view.backgroundColor = UIColor.yellow
        }
    }()
    func testGCD(){
        button.addTo(self.view) { view in
            view.setTitle("asdasdasd", for: UIControl.State.normal)
            view.addTarget(self, action: #selector(labelClick(sender:)), for: UIControl.Event.touchUpInside)
            view.snp.makeConstraints { make in
                make.edges.equalToSuperview()
            }
        }
    }
    @objc func labelClick(sender : UILabel){
        print("current Thread " , Thread.current)
        button.setTitle("开始请求", for: UIControl.State.normal)
        DispatchQueue.global().async {
            print("start 1 " , Thread.current)
            DispatchQueue.main.async {
                print("start 2 " , Thread.current)
                self.button.setTitle("请求结束", for: UIControl.State.normal)
            }
        }
        
    }
    
}
